#if VS_TESSELLATION

#include "CommonTessellation.cfi"

///////////////// hull shader //////////////////

HS_CONSTANT_DATA_OUTPUT IllumConstantsHS(InputPatch<vert2FragGeneral, 3> p, uint PatchID : SV_PrimitiveID)
{
	const float3 vP0 = p[0].vView.xyz + g_VS_WorldViewPos.xyz;
	const float3 vP1 = p[1].vView.xyz + g_VS_WorldViewPos.xyz;
	const float3 vP2 = p[2].vView.xyz + g_VS_WorldViewPos.xyz;
	HS_CONSTANT_DATA_OUTPUT output = CommonConstantsHS(vP0, vP1, vP2, g_VS_WorldViewPos.xyz, g_VS_WorldViewPos.xyz, PatchID);

	return output;
}

[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("IllumConstantsHS")]
[maxtessfactor(MAX_TESS_FACTOR)]
HS_CONTROL_POINT_OUTPUT_ILLUM IllumHS(InputPatch<vert2FragGeneral, 3> inputPatch, uint uCPID : SV_OutputControlPointID)
{
	HS_CONTROL_POINT_OUTPUT_ILLUM	output = (HS_CONTROL_POINT_OUTPUT_ILLUM)0;

	output.controlPoint.vView      = inputPatch[uCPID].vView;
	output.controlPoint.vNormal    = inputPatch[uCPID].vNormal.xyz;
	output.controlPoint.vBaseTC    = inputPatch[uCPID].baseTC;
#if %_RT_FOG || %ANISO_SPECULAR || %MULTILAYER || %IRIS_PARALAX_MAPPING || %OFFSET_BUMP_MAPPING || %PARALLAX_OCCLUSION_MAPPING
	output.vTangent                = inputPatch[uCPID].vTangent;
	output.vBinormal               = inputPatch[uCPID].vBinormal;
#endif
#if %VERTCOLORS || %DIRTLAYER || %BLENDLAYER
	output.Color                   = inputPatch[uCPID].Color;
#endif
#if %_RT_FOG ||%_RT_DECAL_TEXGEN_2D || %DECAL
	output.Ambient                 = inputPatch[uCPID].Ambient;
#endif
	output.screenProj              = inputPatch[uCPID].screenProj;
#if %_LT_LIGHTS && %_LT_HASPROJ  
	output.projTC                  = inputPatch[uCPID].projTC;
#endif    
#if %_RT_DECAL_TEXGEN_2D 
	output.DistAtten               = inputPatch[uCPID].DistAtten;
#endif
#if %_RT_FOG && !%_RT_DECAL_TEXGEN_2D
	output.AvgFogVolumeContrib     = inputPatch[uCPID].AvgFogVolumeContrib;
#endif

	return output;
}

///////////////// domain shader //////////////////

void FillEvalInput(in HS_CONTROL_POINT_OUTPUT_ILLUM triPatch[3], in HS_CONSTANT_DATA_OUTPUT hsConstData, in float3 vBaryCoords, inout CommonEvaluationInputDS evalInput)
{
	evalInput.vBaryCoords = vBaryCoords;
	FillEvalInputControlPoint(triPatch[0].controlPoint, triPatch[1].controlPoint, triPatch[2].controlPoint, hsConstData, evalInput);
	evalInput.bRelativePos = true;
#if %_RT_FOG || %ANISO_SPECULAR || %MULTILAYER || %IRIS_PARALAX_MAPPING || %OFFSET_BUMP_MAPPING || %PARALLAX_OCCLUSION_MAPPING
	FillEvalInputTangentBinormal(triPatch[0].vTangent, triPatch[1].vTangent, triPatch[2].vTangent, triPatch[0].vBinormal, triPatch[1].vBinormal, triPatch[2].vBinormal, evalInput);
#endif
#if %VERTCOLORS || %DIRTLAYER || %BLENDLAYER
	FillEvalInputColor(triPatch[0].Color, triPatch[1].Color, triPatch[2].Color, evalInput);
#endif
	evalInput.vViewPos = g_VS_WorldViewPos.xyz;
}

void ProcessEvalOutput(in CommonEvaluationOutputDS evalOutput, inout vert2FragGeneral dsOutput)
{
	dsOutput.baseTC = evalOutput.vBaseTC;
	dsOutput.vView = evalOutput.vPos;
#if %_RT_FOG || %ANISO_SPECULAR || %MULTILAYER || %IRIS_PARALAX_MAPPING || %OFFSET_BUMP_MAPPING || %PARALLAX_OCCLUSION_MAPPING || %SILHOUETTE_PARALLAX_OCCLUSION_MAPPING
	dsOutput.vTangent = evalOutput.vTangent;
	dsOutput.vBinormal = evalOutput.vBinormal;
#endif
#if %VERTCOLORS || %DIRTLAYER || %BLENDLAYER
	dsOutput.Color = evalOutput.vColor;
#endif

	dsOutput.HPosition = mul(g_VS_ViewProjZeroMatr, float4(evalOutput.vPos.xyz, 1));
}

[domain("tri")]
vert2FragGeneral IllumDS(HS_CONSTANT_DATA_OUTPUT hsConstData, float3 vBaryCoords : SV_DomainLocation, const OutputPatch<HS_CONTROL_POINT_OUTPUT_ILLUM, 3> TrianglePatch )
{
	vert2FragGeneral output = (vert2FragGeneral)0;

	CommonEvaluationInputDS evalInput = (CommonEvaluationInputDS)0;
	FillEvalInput(TrianglePatch, hsConstData, vBaryCoords, evalInput);

	CommonEvaluationOutputDS evalOutput;
	Evaluate(evalInput, evalOutput);
	ProcessEvalOutput(evalOutput, output);

#if %_RT_FOG ||%_RT_DECAL_TEXGEN_2D || %DECAL
	output.Ambient = EvalVec(TrianglePatch[0].Ambient, TrianglePatch[1].Ambient, TrianglePatch[2].Ambient, vBaryCoords);
#endif
#if %_RT_FOG && !%_RT_DECAL_TEXGEN_2D
	output.AvgFogVolumeContrib = EvalVec(TrianglePatch[0].AvgFogVolumeContrib, TrianglePatch[1].AvgFogVolumeContrib, TrianglePatch[2].AvgFogVolumeContrib, vBaryCoords);
#endif
	output.screenProj = HPosToScreenTC(output.HPosition);

	return output;
}

#endif
